<?php
	class link_hba extends MY_Controller {
		function link_hba() {
			parent::__construct();
			@session_start();
			$this->mod_ad->is_logged_in();		
			$_SESSION['redirect'] = $this->uri->uri_string();
			//kiem tra quyen truy cap 
			$this->check_permission();
		}
		
		function index(){
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Liên kết HBA";
			$data['table'] = 'link_hba';
			$data['controller'] = 'link_hba';
			$data['content_admin'] = 'back_end/link_hba/index';
			$data['sort'] = 'ASC';
			$this->db->order_by('parent_id','asc');
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			$per_page = 10;
			$uri = 4;
				
			if(isset($_SESSION['order_sort_link_hba'])) {
				$data['link_hba_value'] = $this->mod_lib->load_all('link_hba','',array(),$per_page,$this->uri->segment($uri),$_SESSION['order_sort_link_hba']);
			} else {
				$data['link_hba_value'] = $this->mod_lib->load_all('link_hba','',array(),$per_page,$this->uri->segment($uri),array("order"=>"asc"));
			}	
				
			$config['base_url']     =  site_url('back_end/link_hba/index/');
			$config['total_rows']   =  $this->db->count_all('link_hba');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			
			$this->pagination->initialize($config);
			$this->load->view('back_end/template_backend',$data);	
		}
		
		function load (){
			$value_all = $this->input->POST('value_all');

			if (!empty($value_all)) {
				foreach ($value_all as $value_all_vl_arr) {
					if(Is_Numeric($value_all_vl_arr)){
						//lay ra order cua ban ghi bi xoa
						$this->db->select('order');
						$admin_menu_one = $this->db->get_where('link_hba',array('id' => $value_all_vl_arr))->row_array();
						$order_one = $admin_menu_one['order'];
						
						//xoa admin menu
						$this->db->delete('link_hba', array('id' => $value_all_vl_arr));
						
						//lay ra cac ban ghi co gia tri order lon hon gia tri order cua ban ghi da xoa
						$this->db->select('id,order');
						$this->db->where("order > $order_one");
						$this->db->order_by("order", "asc");
						$category_product_arr = $this->db->get('link_hba')->result_array();
						
						//cong them moi gia order 1 don vi
						foreach ($category_product_arr as $category_product_arr_value) {
							$where = array(
						  			'id' => $category_product_arr_value['id']
						  		);		  		
						  	$order = array();
							$data_value = array (
									'order'       => $category_product_arr_value['order'] - 1
								);
							
							$this->mod_lib->update('link_hba',$where,$order,$data_value);
						}
						
						$data['success_vl'] = 'Xóa thành công !'; 
						//$this->session->set_flashdata('msview', 'Xóa sản phẩm thành công !');
					}		
				}
			}
			
			$data['vl_sort'] = "DESC";	
			$data['title'] = "Quản lý liên kết HBA";
			$data['table'] = 'link_hba';
			$data['controller'] = 'link_hba';
			$data['sort'] = 'ASC';
			$data['content_admin'] = 'back_end/link_hba/load';
			$per_page = 20;
			$uri = 4;
			$data['link_hba_value'] = $this->mod_lib->load_all('link_hba','',array(),$per_page,$this->uri->segment($uri),array("order"=>"asc"));																			
			$config['base_url']     =  site_url('back_end/link_hba/index/');
			$config['total_rows']   =  $this->db->count_all_results('link_hba');
			$config['per_page']     =  $per_page; 
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);

			
			$this->load->view('back_end/link_hba/load',$data);
		}
		
		function order() {
			//lay ra cac danh muc
			$this->db->order_by('id','asc');
			$data['category_admin'] = $this->db->get('link_hba')->result_array();
			$order_link_hba = $this->input->post('order_link_hba');
			
			//dem so danh muc 
			$count_order = count($data['category_admin']);
			$i = 0;
				
			foreach ($order_link_hba as $order_link_hba_key => $order_link_hba_value) {
				//admin menu can thay doi order 
				$this->db->select('id,order');
				$data['order_adm'] = $this->db->get_where('link_hba',array('id' => $order_link_hba_key))->row_array();
				
				//link_hba trung voi gia tri can thay doi
				$this->db->select('id,order');
				$data['order_adm_value'] = $this->db->get_where('link_hba',array('order' => $order_link_hba_value))->row_array();
					
				//hoa doi gia tri order
				if (!empty($data['order_adm_value'])) {
					if ($data['order_adm']['id'] !== $data['order_adm_value']['id']) {
						$where_1 = array(
			  				'id' => $data['order_adm']['id']
			  			);
			  			
			  			$order_1 = array(
			  				'id' => 'DESC' 
			  			);
			  	  
						$data_value_1 = array (
							'order' => $data['order_adm_value']['order'],
						);	
						
						$this->mod_lib->update('link_hba',$where_1,$order_1,$data_value_1);
						
						$where_2 = array(
			  				'id' => $data['order_adm_value']['id']
			  			);
			  			
			  			$order_2 = array(
			  				'id' => 'DESC' 
			  			);
			  	  		
						$data_value_2 = array (
							'order' => $data['order_adm']['order'],
						);	
						
						$this->mod_lib->update('link_hba',$where_2,$order_2,$data_value_2);
						
						$this->session->set_flashdata('msview', 'Thay đổi thứ tự thành công !');
						redirect('back_end/link_hba');
					} 		
				}	
			}
			
			redirect('back_end/link_hba');
		}	
		
		function active_kh($id = Null) {
			// sua ban ghi can active
			$where_active = array(
			  			'id' => $id 
			  		);
		  	$order_active = array();
			$data_value_active = array (
						'active' => 1	
					);
			
			$this->mod_lib->update('link_hba',$where_active ,$order_active ,$data_value_active);
			
			//thay doi cac ban ghi ko active
			$active = $this->db->get('link_hba')->result_array();
			foreach ($active as $active_arr) {
				if ($active_arr['id'] !== $id) {
					$where = array(
				  			'id' => $active_arr['id']
				  		);
				  	$order = array();
					$data_value = array (
								'active' => 0	
							);
					
					$this->mod_lib->update('link_hba',$where ,$order ,$data_value);
				}	
			}
			
			redirect('back_end/link_hba');
		}
		
		function search () {
			$data['vl_sort'] = "DESC";	
			$check_search = $this->input->POST('formSearch');
			$per_page = 10;
			$data['title'] = "Liên kết HBA - Search";
			$data['table'] = 'link_hba';
			$data['controller'] = 'link_hba';
			$data['sort'] = 'ASC';
			$this->db->order_by('parent_id','asc');
			$data['content_admin'] = 'back_end/link_hba/index';
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai	
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			if(!empty($check_search)){
				$_SESSION['like_link_hba'] = array('name' =>$this->input->POST('nameSearch'));																												
			} else {
				$_SESSION['like_link_hba'] = array();
			}
			
			$data_search = $this->mod_lib->search('link_hba','',$_SESSION['like_link_hba'],array(),array("id"=>"desc"));
			$data['link_hba_value'] = $this->mod_lib->load_search('link_hba','',$_SESSION['like_link_hba'],array(),$per_page,$this->uri->segment(4),array("id"=>"desc"));		
			$config['base_url']     =  site_url('back_end/link_hba/search/');
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  4; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);	
		}
		
		function sort($field,$sort = '') {
			$per_page = 10;
			$uri = 4;
			$data['title'] = "Sắp xếp liên kết HBA";
			$data['table'] = 'link_hba';
			$data['controller'] = 'link_hba';
			$data['content_admin'] = 'back_end/link_hba/index';
			$this->db->order_by('parent_id','asc');
			
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			
			$uri_value_4 =  $this->uri->segment(4);
			$uri_value_5 =  $this->uri->segment(5);
			
			$_SESSION['order_sort_link_hba'] = array(
				$uri_value_4 => $uri_value_5 
			);
			
			$_SESSION['sort'] = $sort;
			$_SESSION['field'] = $field;
			
			if ($sort == 'DESC') {
				$data['sort'] =  'ASC';
			} else if($sort == 'ASC') {
				$data['sort'] =  'DESC';
			}
			
			$data_search = $this->mod_lib->search('link_hba','',array(),array(),array("id"=>"desc"));
			$data['link_hba_value'] = $this->mod_lib->load_search('link_hba','',array(),array(),$per_page,$this->uri->segment($uri),array($_SESSION['field']=>$_SESSION['sort']));
			
			$config['base_url']     =  site_url('back_end/link_hba/index');
			$config['total_rows']   =  count($data_search);
			$config['per_page']     =  $per_page;
			$config['uri_segment']  =  $uri; 
			$this->pagination->initialize($config);		
			$this->load->view('back_end/template_backend',$data);
		}
		
		function add(){
			$data['title']		   = "Thêm liên kết mới";
			$data['content_admin'] = 'back_end/link_hba/add';
			$data['table'] = 'link_hba';
			$add = $this->input->post('addform');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
			//lay ra so order 
			$controller_link_hba = $this->db->get('link_hba')->result_array();
					
			if(!empty($add)){ 	
				$data['link_hba_value'] = $this->db->get('link_hba')->result_array();
				$data['content_admin'] = 'back_end/link_hba/index';

				$data_value = array (
							'name'       => trim($this->input->post('name')),
							'link'       => trim($this->input->post('link')),
							'order'      => count($controller_link_hba) + 1,
							'status'     => trim($this->input->post('status'))		
						);
				
				if($this->mod_lib->insert('link_hba',$data_value)) {				
					$this->session->set_flashdata('msview', 'Thêm thành công !');
					$this->mod_lib->insert_log('Add link_hba',$_SESSION['admin_id']);
				} else {	
					$this->session->set_flashdata('msview', 'Thêm thất bại !');
					$this->load->view('back_end/template_backend',$data);
				}
							
				redirect('back_end/link_hba');			
			} else {
				
				$this->load->view('back_end/template_backend',$data);
			}
		}
		
		function edit($id=null){
			$data['title']			= "Sửa liên kết mới";
			$data['content_admin'] = 'back_end/link_hba/edit';
			$data['link_hba_value'] = $this->db->get_where('link_hba',array( 'id' => $id ))->row_array();
			$edit = $this->input->post('editForm');
			$this->db->order_by('parent_id','asc');
			
			//lay ra danh muc menu theo thu tu sap xep
			$data['table'] = 'link_hba';
			$add = $this->input->post('addform');
			//lay ra cac danh muc cap 1
			$data['category_admin_1'] = $this->db->get_where('admin_menu',array('level' => 1))->result_array();
			//lay ra cac danh muc cap 2
			$data['category_admin_2'] = $this->db->get('admin_menu',array('level' => 2))->result_array();
			//lay ra category hien tai
			$uri_value = $this->uri->segment(2);
			$this->db->select('root_path,id');
			$data['category_admin_active'] = $this->db->get_where('admin_menu',array('controller' => $uri_value))->row_array();
					
			if(!empty($edit)){					
				$where = array(
			  			'id' => $id 
			  		);
			  	$order = array(
			  			'id' => 'DESC' 
			  		);

				$data_value = array (
						'name'       => trim($this->input->post('name')),
						'link'       => trim($this->input->post('link')),
						'status'     => trim($this->input->post('status'))		
					);
			  	
				if($this->mod_lib->update('link_hba',$where,$order,$data_value)){
					$this->session->set_flashdata('msview', 'Sửa thành công !');
				} else {
					$this->session->set_flashdata('msview', 'Sửa thất bại !');
				}
				
				redirect('back_end/link_hba');
			}else {		
				$this->load->view('back_end/template_backend',$data);		
			}
		} 
		
		function delete($id = null) {
			//lay ra order cua ban ghi bi xoa
			$this->db->select('order');
			$link_hba_one = $this->db->get_where('link_hba',array('id' => $id))->row_array();
			$order_one = $link_hba_one['order'];
			
			//xoa link_hba
			$this->db->delete('link_hba', array('id' => $id));
			
			//lay ra cac ban ghi co gia tri order lon hon gia tri order cua ban ghi da xoa
			$this->db->select('id,order');
			$this->db->where("order > $order_one");
			$this->db->order_by("order", "asc");
			$link_hba_arr = $this->db->get('link_hba')->result_array();
			
			//cong them moi gia order 1 don vi
			foreach ($link_hba_arr as $link_hba_arr_value) {
				$where = array(
			  			'id' => $link_hba_arr_value['id']
			  		);
			  		
			  	$order = array();
			  	
				$data_value = array (
					'order'       => $link_hba_arr_value['order'] - 1
				);
				
				$this->mod_lib->update('link_hba',$where,$order,$data_value);
			}
			
			$this->session->set_flashdata('msview', 'Xóa thành công !');
			redirect('back_end/link_hba');
		}
		
		function deleteAll() {
			$value_id_arr    = $this->input->post('value_id');
			
			foreach ($value_id_arr as $value_id_arr_key => $value_id_arr_value) {
				//lay ra order cua ban ghi bi xoa
				$this->db->select('order');
				$link_hba_one = $this->db->get_where('link_hba',array('id' => $id))->row_array();
				$order_one = $link_hba_one['order'];
				
				//xoa admin menu
				$this->db->delete('link_hba', array('id' => $id));
				
				//lay ra cac ban ghi co gia tri order lon hon gia tri order cua ban ghi da xoa
				$this->db->select('id,order');
				$this->db->where("order > $order_one");
				$this->db->order_by("order", "asc");
				$link_hba_arr = $this->db->get('link_hba')->result_array();
				
				//cong them moi gia order 1 don vi
				foreach ($link_hba_arr as $link_hba_arr_value) {
					$where = array(
				  			'id' => $link_hba_arr_value['id']
				  		);
				  		
				  	$order = array();
				  	
					$data_value = array (
						'order'       => $link_hba_arr_value['order'] - 1
					);
					
					$this->mod_lib->update('link_hba',$where,$order,$data_value);
				}
				
				//xoa quyen tuong ung cua nguoi dung 
				$this->db->delete('permiss_admins', array('link_hba_id' => $id));
			}
			
			$this->session->set_flashdata('msview', 'Xóa thành công !');	
			redirect('back_end/link_hba');
		}
		
	}
?>